package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.entity.CameraData;
import edu.ynu.se.xiecheng.achitectureclass.service.CameraDataService;
import edu.ynu.se.xiecheng.achitectureclass.service.Ys7Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/camera")
public class CameraController {

    @Autowired
    private CameraDataService cameraDataService;

    @Autowired
    private Ys7Service ys7Service;

    @GetMapping("/getToken")
    public String getToken() {
            String token = ys7Service.getToken();
            return token;
    }

    @PostMapping("/capture")
    public ResponseEntity<?> capturePhoto(@RequestBody Map<String, String> request) {
        try {
            String deviceSerial = request.get("deviceSerial");
            cameraDataService.captureAndSavePhoto(deviceSerial);
            return ResponseEntity.ok("Photo captured and saved successfully.");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/latest-data")
    public ResponseEntity<?> getLatestCameraData() {
        try {
            CameraData data = cameraDataService.getLatestCameraData();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/historical-data")
    public ResponseEntity<?> getHistoricalCameraData() {
        try {
            List<CameraData> data = cameraDataService.getHistoricalCameraData();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

}